﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AtomicTypes;
using NDimArrayNamespace;
namespace QLabValues
{
    public class QBooleanNDimArray : NDimArray<AtomicTypes.QBoolean>, IQLabValue, IIsComplex
    {
        public QBooleanNDimArray()
            : base()
        {
        }

        public QBooleanNDimArray(params int[] dimensionSize)
            : base(dimensionSize)
        {
        }
        public QBooleanNDimArray(QBoolean[] data, params int[] dimensionSize)
            : base(data, dimensionSize)
        {
        }

        //Darko ubacio
        public QBooleanNDimArray(QBooleanNDimArray qba)
            : base(qba.Data, qba.DimensionSize)
        {
        }

        //Darko ubacio
        public QBooleanNDimArray(QLabValues.IQLabValue iqv)
        {
            if (iqv.GetEnumType() == QLabValues.IQLabValueType.QBooleanNDimArray)
            {
                QBooleanNDimArray qba = new QBooleanNDimArray((QBooleanNDimArray)iqv);
                QBoolean[] els = new QBoolean[qba.Data.Count()];
                for (int i = 0; i < qba.Data.Count(); i++)
                    els[i] = new QBoolean(qba.Data[i]);
                this.Data = (QBoolean[])els.Clone();
                this.DimensionSize = (int[])qba.DimensionSize.Clone();
            }
            else
            {
                //do ovog slucaja verovatno nikad nece doci zato sto ce pri koriscenju ovog konstruktora
                //u NumericalCalculations projectu, uvek pre njegovog pozivanja biti pozvana funkcija
                //canConvertListToEnum koja ce proveravati da li dobijeni IQlabValue moze da se konvertuje u ovaj tip
                //i ukoliko ona vrati true, tada ce se konstruktor pozvati
                throw(new Exception("Cannot convert this type to QBooleanArray."));
            }
        }

        public object Clone()
        {
            return (object)new QBooleanNDimArray(Data, DimensionSize); // svo kopiranje se vec vrsi u konstruktoru
        }

        public int[] ToIndexArray()
        {
            List<int> temp = new List<int>();
            for (int i = 0; i < this.Data.Count(); i++)
                temp.Add(Convert.ToInt32(Data[i].Real) - 1);
            return temp.ToArray();
        }

        public QBooleanNDimArray GetSubArray(int[][] subNDimArrayCoordinates)
        {
            NDimArray<QBoolean> 
                tempArray = base.GetSubNDimArray(subNDimArrayCoordinates);
            return new QBooleanNDimArray(tempArray.Data, tempArray.DimensionSize);
        }

        public QBooleanNDimArray SubAssignment(QBooleanNDimArray matrix, int[][] subNDimArrayCoordinates)
        {
            NDimArray<QBoolean> tempArray = base.SubscriptedAssignment(new NDimArray<QBoolean>(matrix.Data, matrix.DimensionSize), subNDimArrayCoordinates);
            return new QBooleanNDimArray(tempArray.Data, tempArray.DimensionSize);
        }

        public QLabValues.IQLabValueType GetEnumType()
        {
            return QLabValues.IQLabValueType.QBooleanNDimArray;
        }

        public bool LogicalValue()
        {
            for (int i = 0; i < Data.Count(); i++)
                if (!Data[i].Real)
                    return false;
            return true;
        }
    }
}
